module executor.friend.server.ApplyContactExecutor;

import msgtrans;
import message.Constants;
import message.MsgDefine;
import google.protobuf;
import hunt.logging;
import hunt.util.Serialize;
import client.MsgServerClient;
import client.ImClient;
import client.LogicServerClient;
import std.array;
import std.conv : to;
import core.stdc.time;

@TransportServer(gateServerName)
class ApplyContactExecutor : AbstractExecutor!(ApplyContactExecutor){

  @MessageId(MESSAGE.CMD_APPLY_FRIEND_REQ)
  void applyFriend(TransportContext ctx, MessageBuffer buffer) {

    auto req = new ApplyContact();
    buffer.data.fromProtobuf!ApplyContact(req);
    req.timestamp = core.stdc.time.time(null);

    ApplyContactAck ack = new ApplyContactAck;
    ack.succeeded = true;
    ack.tagId = req.tagId;
    ctx.session().send(new MessageBuffer(MESSAGE.CMD_APPLY_FRIEND_ACK,ack.toProtobuf.array));

    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_APPLY_FRIEND_REQ,req.toProtobuf.array));
    logInfo("applyFriend ---- %s ---- %s" ,req.fromId,req.toId);
  }

  @MessageId(MESSAGE.CMD_DEAL_FRIEND_REQ)
  void dealApplyContact(TransportContext ctx, MessageBuffer buffer) {
    auto req = new DealApplyContact();
    buffer.data.fromProtobuf!DealApplyContact(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_DEAL_FRIEND_REQ,req.toProtobuf.array));
    logInfo("dealApplyContact ----");
  }

  @MessageId(MESSAGE.CMD_DELETE_FRIEND_REQ)
  void deleteFriend(TransportContext ctx, MessageBuffer buffer) {
    auto req = new DeleteContactREQ();
    buffer.data.fromProtobuf!DeleteContactREQ(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_DELETE_FRIEND_REQ,req.toProtobuf.array));
    logInfo("deleteFriend ----");
  }

  @MessageId(MESSAGE.CMD_SET_CONTACTS_ALIAS_NAME_REQ)
  void setContactAliasName(TransportContext ctx, MessageBuffer buffer) {
    auto req = new SetContactAliasNameREQ();
    buffer.data.fromProtobuf!SetContactAliasNameREQ(req);
    LogicServerClient.getInstance().getForwardClient.send(new MessageBuffer(MESSAGE.CMD_SET_CONTACTS_ALIAS_NAME_REQ,req.toProtobuf.array));
    logInfo("setContactAliasName ----");
  }
}

